package com.titan.demo.camel.sample.component.stream;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.junit.Assert;

import com.titan.demo.camel.utils.RunCamel;


/**
 * run1:  使用控制台输出 body 内容
 * run2:  比较输出内容
 */
public class Test02 {
	private static final String message = "Hello World";
	
	public static void main(String[] args) {
		try {
			run1();
			run2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private static void run1() throws Exception {
		new RunCamel() {
			protected void execute(CamelContext context,
					ProducerTemplate producer, ConsumerTemplate consumer)
					throws Exception {
				producer.sendBody("direct:in", message);
			}
		}.run(createRouteBuilder());
	}
	private static void run2() throws Exception {
		new RunCamel() {
			protected void execute(CamelContext context,
					ProducerTemplate producer, ConsumerTemplate consumer)
					throws Exception {
				ByteArrayOutputStream mockOut = new ByteArrayOutputStream();
				System.setOut(new PrintStream(mockOut));

				producer.sendBody("direct:in", message);
				
				Assert.assertEquals(message + "\n", new String(mockOut.toByteArray()));
			}
		}.run(createRouteBuilder());
	}
	
	
	private static RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:in").to("stream:out");
            }
        };
    }	
}
